package com.erp.erp_ui.ProductMainInfo.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.ProductMainInfo.Req.*;
import com.erp.erp_entitys.ProductMainInfo.Res.*;
import com.erp.erp_servers.ProductMainInfo.IProductMainInfoService;
import com.erp.utils.DateUtils;
import com.erp.utils.StringUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zjk
 * @version 1.0
 * @description
 * @date 2021-08-01 5:42
 */
@Service
public class productMainInfoAllService {

    private final IProductMainInfoService productMainInfoService;

    public productMainInfoAllService(IProductMainInfoService productMainInfoService) {
        this.productMainInfoService = productMainInfoService;
    }
    /**
     * 获取String类型不为空数据
     *
     * @param value 目标数据
     * @return 不为空数据
     */
    private String getNotNullOfStringInfo(String value) {
        if (StringUtils.isEmpty(value)) {
            return "";
        } else {
            return value;
        }
    }

    /**
     * 获取BigDecimal类型不为空的数据
     *
     * @param value 目标数据
     * @return 不为空数据
     */
    private BigDecimal getNotNullOfBigDecimalInfo(BigDecimal value) {
        if (StringUtil.isNotEmpty(value)) {
            return value;
        } else {
            return new BigDecimal("0");
        }
    }
    /**
     * 产品详情-分页
     * @param req
     * @return
     */
    public PageInfo<productDetailStatisticsRes> productDetailStatisticsPage(productDetailStatisticsReq req){
        List<productDetailStatisticsRes> list=productMainInfoService.productDetailStatisticsList(req);
        PageInfo pageInfo=new PageInfo(list);
        pageInfo.setList(list);
        return pageInfo;
    }

    /**
     * 产品详情--导出
     * @param req
     * @return
     */
    public JSONObject productDetailStatisticsExport(productDetailStatisticsReq req){
        List<productDetailStatisticsRes> productDetailStatisticsResList=productMainInfoService.productDetailStatisticsList(req);

        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleProductDetailStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(productDetailStatisticsResList)){
            obj.put("data", data);
            return obj;
        }
        //封装数据
        getPoroductDetailStatisticsInfo(data, productDetailStatisticsResList);

        obj.put("data", data);
        return obj;
    }

    private JSONArray getTitleProductDetailStatistics() {
        JSONArray obj = new JSONArray();
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("规格说明");
        obj.add("创建日期");
        obj.add("中文描述");
        obj.add("英文描述");
        obj.add("含税出厂价");
        obj.add("未税出厂价");
        obj.add("含税FOB价");
        obj.add("未税FOB价");
        obj.add("毛重");
        obj.add("净重");
        obj.add("所属目录");
        obj.add("所属目录上级");
        return obj;
    }

    private void getPoroductDetailStatisticsInfo(JSONArray data,List<productDetailStatisticsRes> list){
        for (productDetailStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationExplanation()));
            infoList.add(getNotNullOfStringInfo(vo.getGe()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncludedPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getUntaxedPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncludedFob()));
            infoList.add(getNotNullOfStringInfo(vo.getUntaxedFob()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getGrossWeight()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getNetWeight()));
            infoList.add(getNotNullOfStringInfo(vo.getContents()));
            infoList.add(getNotNullOfStringInfo(vo.getContentsParent()));

            data.add(infoList);
        }
    }


    /**
     * 按厂商产品分组详情-分页
     * @param req
     * @return
     */
    public PageInfo<productGroupDetailStatisticsRes> productGroupDetailStatisticsPage(productGroupDetailStatisticsReq req){
        List<productGroupDetailStatisticsRes> list=productMainInfoService.productGroupDetailStatisticsList(req);
        PageInfo pageInfo=new PageInfo(list);
        pageInfo.setList(list);
        return pageInfo;
    }


    /**
     * 按厂商产品分组详情--导出
     * @param req
     * @return
     */
    public JSONObject productGroupDetailStatisticsExport(productGroupDetailStatisticsReq req){
        List<productGroupDetailStatisticsRes> productDetailStatisticsResList=productMainInfoService.productGroupDetailStatisticsList(req);

        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleProductGroupDetailStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(productDetailStatisticsResList)){
            obj.put("data", data);
            return obj;
        }
        //封装数据
        getPoroductGroupDetailStatisticsInfo(data, productDetailStatisticsResList);

        obj.put("data", data);
        return obj;
    }

    private JSONArray getTitleProductGroupDetailStatistics() {
        JSONArray obj = new JSONArray();
        obj.add("序号");
        obj.add("供应商名称");
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("规格说明");
        obj.add("总数量");
        obj.add("产品单价");
        obj.add("其它费用");
        obj.add("采购成本");
        obj.add("费用备注");
        obj.add("中文描述");
        obj.add("英文描述");
        obj.add("产品所属目录");
        return obj;
    }

    private void getPoroductGroupDetailStatisticsInfo(JSONArray data,List<productGroupDetailStatisticsRes> list){
        //总数量
        BigDecimal totalQuantity = new BigDecimal("0");
        //其它费用
        BigDecimal otherCost = new BigDecimal("0");
        int i = 0;
        for (productGroupDetailStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(++i);
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationExplanation()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getTotalQuantity()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getProductUnitPrice()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getOtherCost()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseCost()));
            infoList.add(getNotNullOfStringInfo(vo.getCostDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getCatalog()));

            if (StringUtil.isNotEmpty(vo.getTotalQuantity())) {
                totalQuantity = totalQuantity.add(vo.getTotalQuantity());
            }
            if (StringUtil.isNotEmpty(vo.getOtherCost())) {
                otherCost = otherCost.add(vo.getOtherCost());
            }
            data.add(infoList);
        }
        //合计
        JSONArray titleList = new JSONArray();
        titleList.add("合计");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add(totalQuantity);
        titleList.add("");
        titleList.add(otherCost);
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        data.add(titleList);

    }


    /**
     * 厂商产品排价-分页
     * @param req
     * @return
     */
    public PageInfo<productPriceDetailStatisticsRes> productPriceDetailStatisticsPage(productPriceDetailStatisticsReq req){
        List<productPriceDetailStatisticsRes> list=productMainInfoService.productPriceDetailStatisticsList(req);
        for(productPriceDetailStatisticsRes p:list){
            p.setCreateDateStr(DateUtils.format(p.getCreateDate(),DateUtils.DATE_PATTERN));
            p.setUpdateDateStr(DateUtils.format(p.getUpdateDate(),DateUtils.DATE_PATTERN));
        }
        PageInfo pageInfo=new PageInfo(list);
        pageInfo.setList(list);
        return pageInfo;
    }
    /**
     * 按厂商产品分组详情--导出
     * @param req
     * @return
     */
    public JSONObject productPriceDetailStatisticsExport(productPriceDetailStatisticsReq req){
        List<productPriceDetailStatisticsRes> productDetailStatisticsResList=productMainInfoService.productPriceDetailStatisticsList(req);
        for(productPriceDetailStatisticsRes p:productDetailStatisticsResList){
            p.setCreateDateStr(DateUtils.format(p.getCreateDate(),DateUtils.DATE_PATTERN));
            p.setUpdateDateStr(DateUtils.format(p.getUpdateDate(),DateUtils.DATE_PATTERN));
        }
        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleProductPriceDetailStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(productDetailStatisticsResList)){
            obj.put("data", data);
            return obj;
        }
        //封装数据
        getPoroductPriceDetailStatisticsInfo(data, productDetailStatisticsResList);

        obj.put("data", data);
        return obj;
    }

    private JSONArray getTitleProductPriceDetailStatistics() {
        JSONArray obj = new JSONArray();
        obj.add("供应商名称");
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("含税出厂价");
        obj.add("未税出厂价");
        obj.add("含税FOB价");
        obj.add("未含税FOB价");
        obj.add("币种");
        obj.add("创建日期");
        obj.add("修改日期");
        return obj;
    }

    private void getPoroductPriceDetailStatisticsInfo(JSONArray data,List<productPriceDetailStatisticsRes> list){
        for (productPriceDetailStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncludedCost()));
            infoList.add(getNotNullOfStringInfo(vo.getUntaxedCost()));
            infoList.add(getNotNullOfStringInfo(vo.getFob()));
            infoList.add(getNotNullOfStringInfo(vo.getCshh()));
            infoList.add(getNotNullOfStringInfo(vo.getCurrencyName()));
            infoList.add(getNotNullOfStringInfo(vo.getCreateDateStr()));
            infoList.add(getNotNullOfStringInfo(vo.getUpdateDateStr()));
            data.add(infoList);
        }
    }


    /**
     * 产品采购Top-分页
     *
     * @param req
     * @return
     */
    public PageInfo<productPurchaseTopStatisticsRes> productPurchaseTopStatisticsPage(productPurchaseTopStatisticsReq req) {
        List<productPurchaseTopStatisticsRes> list = productMainInfoService.productPurchaseTopStatisticsList(req);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setList(list);
        return pageInfo;
    }


    /**
     * 产品采购Top--导出
     * @param req
     * @return
     */
    public JSONObject productPurchaseTopStatisticsExport(productPurchaseTopStatisticsReq req){
        List<productPurchaseTopStatisticsRes> productDetailStatisticsResList=productMainInfoService.productPurchaseTopStatisticsList(req);

        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleProductPurchaseTopStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(productDetailStatisticsResList)){
            obj.put("data", data);
            return obj;
        }
        //封装数据
        getPoroductPurchaseTopStatisticsInfo(data, productDetailStatisticsResList);

        obj.put("data", data);
        return obj;
    }


    private JSONArray getTitleProductPurchaseTopStatistics() {
        JSONArray obj = new JSONArray();
        obj.add("序号");
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("规格说明");
        obj.add("总数量");
        obj.add("中文描述");
        obj.add("英文描述");
        obj.add("产品所属目录");
        return obj;
    }

    private void getPoroductPurchaseTopStatisticsInfo(JSONArray data,List<productPurchaseTopStatisticsRes> list){
        //总数量
        BigDecimal totalQuantity = new BigDecimal("0");
        int i = 0;
        for (productPurchaseTopStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(++i);
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationExplanation()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getTotalQuantity()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getCatalog()));

            if (StringUtil.isNotEmpty(vo.getTotalQuantity())) {
                totalQuantity = totalQuantity.add(vo.getTotalQuantity());
            }
            data.add(infoList);
        }
        //合计
        JSONArray titleList = new JSONArray();
        titleList.add("合计");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add(totalQuantity);
        titleList.add("");
        titleList.add("");
        titleList.add("");
        data.add(titleList);
    }

    /**
     * 产品未录入单价下采购单统计-集合
     * @param req
     * @return
     */
    private List<productNotEnteredPurchaseStatisticsRes> productNotEnteredPurchaseStatistics(productNotEnteredPurchaseStatisticsReq req){
        List<productNotEnteredPurchaseStatisticsRes>  list=productMainInfoService.productNotEnteredPurchaseStatisticsList(req);
        for (productNotEnteredPurchaseStatisticsRes p:list){
            p.setSigningDateStr(DateUtils.format(p.getSigningDate(),DateUtils.DATE_PATTERN));
            if (StringUtils.isNotEmpty(p.getTaxIncluded())){
                switch (p.getTaxIncluded()){
                    case "1":
                        p.setTaxIncluded("含税出厂价");
                        break;
                    case "2":
                        p.setTaxIncluded("未税出厂价");
                        break;
                    case "3":
                        p.setTaxIncluded("含税FOB价");
                        break;
                    case "4":
                        p.setTaxIncluded("未税FOB价");
                        break;
                    default:
                        p.setTaxIncluded("");
                }
            }

        }
        return list;
    }

    /**
     * 产品未录入单价下采购单统计-分页
     * @param req
     * @return
     */
    public PageInfo<productNotEnteredPurchaseStatisticsRes> productNotEnteredPurchaseStatisticsPage(productNotEnteredPurchaseStatisticsReq req){
        List<productNotEnteredPurchaseStatisticsRes> list=productNotEnteredPurchaseStatistics(req);
        PageInfo pageInfo=new PageInfo(list);
        pageInfo.setList(list);
        return pageInfo;
    }


    /**
     * 产品未录入单价下采购单统计--导出
     * @param req
     * @return
     */
    public JSONObject productNotEnteredPurchaseStatisticsExport(productNotEnteredPurchaseStatisticsReq req){
        List<productNotEnteredPurchaseStatisticsRes> productDetailStatisticsResList=productNotEnteredPurchaseStatistics(req);

        JSONObject obj = new JSONObject();
        //获取excel的title信息
        JSONArray titleList = getTitleProductNotEnteredPurchaseStatistics();
        JSONArray data = new JSONArray();
        data.add(titleList);
        if(CollectionUtils.isEmpty(productDetailStatisticsResList)){
            obj.put("data", data);
            return obj;
        }
        //封装数据
        getPoroductNotEnteredPurchaseStatisticsInfo(data, productDetailStatisticsResList);

        obj.put("data", data);
        return obj;
    }

    private JSONArray getTitleProductNotEnteredPurchaseStatistics() {
        JSONArray obj = new JSONArray();
        obj.add("序号");
        obj.add("供应商名称");
        obj.add("是否含税");
        obj.add("签约日期");
        obj.add("产品编码");
        obj.add("规格编码");
        obj.add("中文描述");
        obj.add("英文描述");
        obj.add("产品单价");
        obj.add("采购成本");
        obj.add("含税出厂价");
        obj.add("未税出厂价");
        obj.add("含税FOB价");
        obj.add("未税FOB价");
        return obj;
    }

    private void getPoroductNotEnteredPurchaseStatisticsInfo(JSONArray data,List<productNotEnteredPurchaseStatisticsRes> list){
        //采购成本
        BigDecimal purchaseCost = new BigDecimal("0");
        int i = 0;
        for (productNotEnteredPurchaseStatisticsRes vo : list) {
            JSONArray infoList = new JSONArray();
            infoList.add(++i);
            infoList.add(getNotNullOfStringInfo(vo.getSupplierName()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncluded()));
            infoList.add(getNotNullOfStringInfo(vo.getSigningDateStr()));
            infoList.add(getNotNullOfStringInfo(vo.getProductCode()));
            infoList.add(getNotNullOfStringInfo(vo.getSpecificationCode()));
            infoList.add(getNotNullOfStringInfo(vo.getChineseDesc()));
            infoList.add(getNotNullOfStringInfo(vo.getEnglishDesc()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getProductUnitPrice()));
            infoList.add(getNotNullOfBigDecimalInfo(vo.getPurchaseCost()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncludedPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getUntaxedPrice()));
            infoList.add(getNotNullOfStringInfo(vo.getTaxIncludedFob()));
            infoList.add(getNotNullOfStringInfo(vo.getUntaxedFob()));

            if (StringUtil.isNotEmpty(vo.getPurchaseCost())) {
                purchaseCost = purchaseCost.add(vo.getPurchaseCost());
            }
            data.add(infoList);
        }
        //合计
        JSONArray titleList = new JSONArray();
        titleList.add("合计");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add(purchaseCost);
        titleList.add("");
        titleList.add("");
        titleList.add("");
        titleList.add("");
        data.add(titleList);

    }


}
